package com.imtimmy.android.pda;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

public class Global {

	public static final String PREFERENCES = "settings";
	public static final String KEY_IMAGEPATH = "ImagePath";
	public static final String KEY_SOUNDPATH = "SoundPath";

	public static final int REQUEST_ID_NEWACTIVITY = 100;
	public static final int REQUEST_ID_MODIFYACTIVITY = 150;
	public static final int REQUEST_ID_TAKEIMAGE = 200;
	public static final int REQUEST_ID_RECORDVOICE = 300;
	public static final int REQUEST_CODE_ALARMEVENT = 500;

	public static final String COLUMN_ID = "id";
	public static final String COLUMN_DATE = "date";
	public static final String COLUMN_TITLE = "title";
	public static final String COLUMN_CONTENT = "content";
	public static final String COLUMN_IMAGE = "image_path";
	public static final String COLUMN_SOUND = "sound_path";

	public static ArrayList<ActivityInfo> activityInfos = new ArrayList<ActivityInfo>();

	//public static Context context;
	
	public static Bundle getBundle(ActivityInfo aInfo) {
		int nId = aInfo.getId();
		Long lDate = aInfo.getDate();
		String sTitle = aInfo.getTitle();
		String sContent = aInfo.getContent();
		String sImagePath = aInfo.getImage();
		String sSoundPath = aInfo.getAudio();
		int color = aInfo.getColor();

		Bundle bundle = new Bundle();
		bundle.putInt(DatabaseHelper.COLUMN_ID, nId);
		bundle.putLong(DatabaseHelper.COLUMN_DATE, lDate);
		bundle.putString(DatabaseHelper.COLUMN_TITLE, sTitle);
		bundle.putString(DatabaseHelper.COLUMN_CONTENT, sContent);
		bundle.putString(DatabaseHelper.COLUMN_IMAGE, sImagePath);
		bundle.putString(DatabaseHelper.COLUMN_SOUND, sSoundPath);
		bundle.putInt(DatabaseHelper.COLUMN_COLOR, color);
		return bundle;
	}

	public static ActivityInfo getActivityInfo(Bundle bundle) {
		int nId = bundle.getInt(DatabaseHelper.COLUMN_ID);
		Long lDate = bundle.getLong(DatabaseHelper.COLUMN_DATE);
		String sTitle = bundle.getString(DatabaseHelper.COLUMN_TITLE);
		String sContent = bundle.getString(DatabaseHelper.COLUMN_CONTENT);
		String sImagePath = bundle.getString(DatabaseHelper.COLUMN_IMAGE);
		String sSoundPath = bundle.getString(DatabaseHelper.COLUMN_SOUND);
		int nColor = bundle.getInt(DatabaseHelper.COLUMN_COLOR);
		ActivityInfo aInfo = new ActivityInfo(nId, sTitle, lDate, sContent,
				sImagePath, sSoundPath, nColor);
		return aInfo;
	}

	public static int getPostionByDate(long date) {
		if (activityInfos == null)
			return -1;
		int length = activityInfos.size();
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		Calendar calendar_of_aInfo = Calendar.getInstance();
		for (int i = 0; i < length; i++) {
			long aInfo_date = activityInfos.get(i).getDate();
			calendar_of_aInfo.setTimeInMillis(aInfo_date);
			int year_of_aInfo = calendar_of_aInfo.get(Calendar.YEAR);
			if (year == year_of_aInfo) {
				int month_of_aInfo = calendar_of_aInfo.get(Calendar.MONTH);
				if (month == month_of_aInfo) {
					int day_of_aInfo = calendar_of_aInfo
							.get(Calendar.DAY_OF_MONTH);
					if (day == day_of_aInfo)
						return i;
				}
			}
		}

		return -1;
	}

	public static Comparator<ActivityInfo> sortByDate = new Comparator<ActivityInfo>() {

		@Override
		public int compare(ActivityInfo aInfo1, ActivityInfo aInfo2) {
			// TODO Auto-generated method stub
			Long date1 = aInfo1.getDate();
			Long date2 = aInfo2.getDate();

			return date2.compareTo(date1);
		}
	};
	
	public static void insertData(ActivityInfo aInfo, Context context) {
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();

		int nId = aInfo.getId();
		Long lDate = aInfo.getDate();
		String sTitle = aInfo.getTitle();
		String sContent = aInfo.getContent();
		String sImagePath = aInfo.getImage();
		String sSoundPath = aInfo.getAudio();
		int color = aInfo.getColor();

		cv.put("id", nId);
		cv.put("date", lDate);
		cv.put("title", sTitle);
		cv.put("content", sContent);
		cv.put("image_path", sImagePath);
		cv.put("sound_path", sSoundPath);
		cv.put("color", color);
		db.insert(DatabaseHelper.TABLE_NAME, null, cv);
		cv.clear();

		db.close();
		dbHelper.close();
	}

	public static void deleteData(ActivityInfo aInfo, Context context) {
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		String where = "id=" + aInfo.getId();
		db.delete(DatabaseHelper.TABLE_NAME, where, null);
		db.close();
		dbHelper.close();
	}

	public static void updateData(ActivityInfo aInfo, Context context) {
		DatabaseHelper dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();

		int nId = aInfo.getId();
		Long lDate = aInfo.getDate();
		String sTitle = aInfo.getTitle();
		String sContent = aInfo.getContent();
		String sImagePath = aInfo.getImage();
		String sSoundPath = aInfo.getAudio();
		int color = aInfo.getColor();

		// cv.put("id", nId);
		cv.put("date", lDate);
		cv.put("title", sTitle);
		cv.put("content", sContent);
		cv.put("image_path", sImagePath);
		cv.put("sound_path", sSoundPath);
		cv.put("color", color);

		String where = "id=" + nId;
		db.update(DatabaseHelper.TABLE_NAME, cv, where, null);
		cv.clear();

		db.close();
		dbHelper.close();
	}
}
